﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MySql.Data.MySqlClient;
namespace test
{
    public partial class FormRoom : Form
    {
        public User user;
        DataSet ds = new DataSet();
        public FormRoom()
        {
            InitializeComponent();
        }
        //窗体加载时填充数据
        private void FormSuperMarket_Load(object sender, EventArgs e)
        {
            FillRoomInfo();
        }
        ///填充DataGridView控件中的房屋
        private void FillRoomInfo()
        {
            ds = new DataSet();
            DBHelper dBHelper = new DBHelper();
            try
            {
                //编写mysql语句
                String sql = "select id 房屋编号, name 房屋名称, roomsize 房屋类型, price 房屋价格, leased 已租出房间, surplus 剩余房间, is_full 是否住满 from hotel";
                //创建适配器
                MySqlDataAdapter adapter = new MySqlDataAdapter(sql, dBHelper.Connection);
                //填充数据到数据集
                adapter.Fill(ds, "Room");
                //绑定数据源
                this.dataGridView1.DataSource = this.ds.Tables["Room"];

            }
            catch (Exception e)
            {
                MessageBox.Show("获取商品列表失败！"+e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);

            }
        }
        //商品显示退出事件
        private void btn_Exit_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }
        //商品显示增加事件
        private void btn_Add_Click(object sender, EventArgs e)
        {
            FormEditSort form = new FormEditSort();
            //新增选中的id
            form.id = 0;
            form.ShowDialog();
            //新增后重新刷新列表
            this.FillRoomInfo();
        }
        //修改功能
        private void btn_Update_Click(object sender, EventArgs e)
        {
            FormEditSort form = new FormEditSort();
            //获取选中的id
            form.id = Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value);
            form.ShowDialog();
            this.FillRoomInfo();

        }
        //删除按钮
        private void btn_Delete_Click(object sender, EventArgs e)
        {
            DeleteRoom();
        }
        //删除按钮的事件
        private void DeleteRoom()
        {
            //先判断是否选中一个id
            if (this.dataGridView1.CurrentRow != null)
            {
                //判断是否删除，并返回一个DialogResult
                DialogResult dr = MessageBox.Show("确定要删除名称为" + dataGridView1.CurrentRow.Cells[1].Value,"系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
                if (dr == DialogResult.OK)
                {
                    DBHelper dBHelper = new DBHelper();
                    try
                    {
                        //动态sql语句
                        StringBuilder sb = new StringBuilder();
                        sb.AppendFormat("delete from hotel where id = {0}", Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value));
                        //执行工具
                        MySqlCommand cmd = new MySqlCommand(sb.ToString(), dBHelper.Connection);
                        //打开数据库连接
                        dBHelper.OpenConnection();
                        //执行
                        int result = cmd.ExecuteNonQuery();
                        //判断是否执行成功
                        if (result == 1)
                        {
                            MessageBox.Show("删除成功！");
                            this.FillRoomInfo();
                        }
                    }
                    catch (Exception e)
                    {
                        MessageBox.Show("删除失败！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                    finally
                    {
                        dBHelper.CloseConnection();
                    }
                }
                else
                {
                    MessageBox.Show("请选择一个民宿！");
                }
            }
        }
        //从DataSet中过滤数据，判断是否住满
        private void Filter(Boolean is_null)
        {
            DataView dv = new DataView(ds.Tables[0]);
            if (is_null == false)
            {
                dv.RowFilter = "是否住满='否'";
            }
            else
            {
                dv.RowFilter = "是否住满='是'";
            }
            this.dataGridView1.DataSource = dv;
        }
        //选择被更改触发该事件
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Text == "所有房屋")//根节点
            {
                this.dataGridView1.DataSource = ds.Tables[0];
            }
            if (e.Node.Text == "已住满")
            {
                this.Filter(true);
            }
            if(e.Node.Text == "未住满")
            {
                 this.Filter(false);
            }
            if (e.Node.Text == "房屋类别")
            {
                FormRoomSort form = new FormRoomSort();
                form.ShowDialog();
            }
            if (e.Node.Text == "入住信息")
            {
                ListInfo form = new ListInfo();
                form.ShowDialog();
            }
            if (e.Node.Text == "评价信息")
            {
                FormCommentInfo form = new FormCommentInfo();
                form.ShowDialog();
            }
            if (e.Node.Text == "修改资料")
            {
                FormChangeInfo form = new FormChangeInfo();
                form.user = user;
                form.ShowDialog();
            }
            if (e.Node.Text == "关于")
            {
                FormAbout form = new FormAbout();
                form.ShowDialog();
            }
            if (e.Node.Text == "修改密码")
            {
                FormChangePassword form = new FormChangePassword();
                form.ShowDialog();
            }
            if (e.Node.Text == "注销账户")
            {
                DeleteUserName();
                //Form1 form = new Form1();
                //form.ShowDialog();
            }
        }

        private void btn_Search_Click(object sender, EventArgs e)
        {
            if (CheckInput())
            {
                SearchRoomName();
            }
        }
        //非空验证
        private bool CheckInput()
        {
            if (text_Search.Text.Trim() == "")
            {
                MessageBox.Show("请输入名称！");
                this.FillRoomInfo();
                return false;
            }
            return true;
        }
        ///查找房屋
        private void SearchRoomName()
        {
            ds = new DataSet();
            String name = text_Search.Text.Trim();
            DBHelper dBHelper = new DBHelper();
            try
            {
                //1.创建sql语句
                String sql = String.Format("select id 房屋编号, name 房屋名称, roomsize 房屋类型, price 房屋价格, leased 已租出房间, surplus 剩余房间, is_full 是否住满 from hotel where name like '{0}'", "%"+name+"%");
                //2.command工具
                MySqlCommand cmd = new MySqlCommand(sql, dBHelper.Connection);
                //创建适配器
                MySqlDataAdapter adapter = new MySqlDataAdapter(sql, dBHelper.Connection);
                //填充数据到数据集
                adapter.Fill(ds, "RoomName");
                //绑定数据源
                this.dataGridView1.DataSource = this.ds.Tables["RoomName"];
            }
            catch (Exception e)
            {
                MessageBox.Show("查找失败：" + e.Message);
            }
        }
        //注销账户
        private void DeleteUserName()
        {
            //判断是否删除，并返回一个DialogResult
            DialogResult dr = MessageBox.Show("确定要注销名称为" + user.Username+"吗", "系统提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);
            if (dr == DialogResult.OK)
            {
                DBHelper dBHelper = new DBHelper();
                try
                {
                    //动态sql语句
                    StringBuilder sb = new StringBuilder();
                    sb.AppendFormat("delete from user where username = {0}", user.Username);
                    //执行工具
                    MySqlCommand cmd = new MySqlCommand(sb.ToString(), dBHelper.Connection);
                    //打开数据库连接
                    dBHelper.OpenConnection();
                    //执行
                    int result = cmd.ExecuteNonQuery();
                    //判断是否执行成功
                    if (result == 1)
                    {
                        MessageBox.Show("删除成功！");
                        this.Hide();
                        Form1 form = new Form1();
                        form.ShowDialog();
                    }
                }
                catch (Exception e)
                {
                    MessageBox.Show("删除失败！" + e, "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                finally
                {
                    dBHelper.CloseConnection();
                }
            }
        }
    }
}
